/*
 * @Author: your name
 * @Date: 2022-03-31 21:35:02
 * @LastEditTime: 2023-07-21 15:54:05
 * @LastEditors: YNT 2501512800@qq.com
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \22-03-19 智慧社区\pages\index\components\activity\index.js
 */
// pages/index/components/activity/index.js
import Activity from '../../../../api/activity'
import dayjs from '../../../../utils/dayjs'
Component({
  options: {
    styleIsolation: 'shared',
  },
  properties: {},
  data: {
    nvabarData: {
      navigationBarTextStyle: 'white', // 胶囊主题 white || black
      navigationBarBackground: '#FEFEFE', // 导航栏背景色
      navigationBarTitleText: '活动',
      hideCapsule: true,
    },

    actList: [],
    page: 1,
    totalPage: 2,
    activity_type_id: '',
    scrollTop: 0,
    scrollHeight: 0,
  },

  lifetimes: {
    attached: function () {
      wx.showLoading()
      let _that = this

      Activity.actTypeList()
        .then(({ data }) => {
          let tabs = data.map((item) => {
            return { name: item.name, id: item.id }
          })
          tabs.unshift({ name: '未结束', id: -1 })

          _that.setData({
            tabs,
            activity_type_id: tabs[0].id,
          })
        })
        .catch(({ errCode, data }) => {})
        .finally(() => {
          this.getActList()
          wx.hideLoading()
        })
    },
  },

  methods: {
    onChange(e) {
      let target = this.data.tabs.filter((item) => {
        return item.name == e.detail.title
      })
      this.setData({
        activity_type_id: target[0].id,
        page: 1,
        totalPage: 2,
        actList: [],
      })

      this.getActList()
    },

    getActList(id) {
      let _that = this

      let actList = _that.data.actList
      let page = _that.data.page
      let totalPage = _that.data.totalPage
      let activity_type_id = _that.data.activity_type_id

      if (activity_type_id == -1) {
        if (page <totalPage) {
          Activity.actTopicList({ current: 1, page: page, per_page: 6 })
            .then(({ data }) => {
              data.items.forEach(function (item) {
                item.canSign = dayjs().isAfter(item.start_time) //活动是否已经开始
                item.canRegister = //是否可以签到
                  dayjs().isAfter(item.start_time) &&
                  dayjs().isBefore(item.end_time)
                item.start_time = dayjs(item.start_time).format(
                  'YYYY-MM-DD HH:mm'
                )
              })
              for (var i = 0; i < data.items.length; i++) {
                actList.push(data.items[i])
              }

              _that.setData({
                actList,
                page: data.next_num ? data.next_num : data.pages,
                totalPage: data.pages,
              })
            })
            .catch(({ errCode, data }) => {
              _that.setData({
                actList: [],
              })
            })
            .finally(() => {
              wx.hideLoading()
            })
        }
      } else {
        if (page < totalPage) {
          Activity.actTopicList({
            activity_type_id: activity_type_id,
            page: page,
            per_page: 6,
          })
            .then(({ data }) => {
              data.items.forEach(function (item) {
                item.canSign = dayjs().isAfter(item.start_time) //活动是否已经开始
                item.canRegister = //是否可以签到
                  dayjs().isAfter(item.start_time) &&
                  dayjs().isBefore(item.end_time)
                item.start_time = dayjs(item.start_time).format(
                  'YYYY-MM-DD HH:mm'
                )
              })
              for (var i = 0; i < data.items.length; i++) {
                actList.push(data.items[i])
              }

              _that.setData({
                actList,
                page: data.next_num ? data.next_num : data.pages,
                totalPage: data.pages,
              })
            })
            .catch(({ errCode, data }) => {
              _that.setData({
                actList: [],
              })
            })
            .finally(() => {
              wx.hideLoading()
            })
        }
      }
    },

    // 查看详情
    checkDetail(e) {
      let consultId = e.currentTarget.dataset.id
      wx.navigateTo({
        url: `/subPackages/topicPackages/pages/topic/topicNewDetail/index?id=${consultId}`,
      })
    },

    // 滑动到底部
    bindDownLoad() {
      this.getActList()
    },

    scroll(event) {
      // this.setData({
      //   scrollTop:event.detail.scrollTop
      // })
    },

    // 滑动到顶部
    topLoad(event) {},
  },
})
